class CommentsController < ApplicationController
  before_filter :find_post,
    :only => [:index, :show, :new, :create, :edit, :update, :destroy]
  before_filter :find_comment_by_id,
    :only => [:show, :edit, :update, :destroy]
  
  def index
    @comments = @post.comments
  end

  def show
  end

  def new
    @comment = @post.comments.build
  end

  def create
    @comment = @post.comments.build(params[:comment])
    if @comment.save
      redirect_to post_comment_url(@post,@comment)
    else
      render :action => "new"
    end
  end

  def edit
  end
  
  def update
    if @comment.update_attributes(params[:comment])
      redirect_to post_comment_url(@post,@comment)
    else
      render :action => "new"
    end
  end
  
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to post_comments_path{@post} }
      format.xml { head :ok }
    end
  end
  
  private 
    def find_post
      @post = Post.find(params[:post_id])
    end
    
  private
    def find_comment_by_id
      @comment = Comment.find(params[:id])
    end
  
end
